package com.wufun.overseas.tools.filetransfer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.util.Log;

import com.wufun.overseas.tools.socket.entity.SocketConfig;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by tangbl on 2017/12/17 0017.
 */

public class NetWorkConnectedReceiver extends BroadcastReceiver {
    private static final String TAG=NetWorkConnectedReceiver.class.getSimpleName();
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {//监听wifi 打开关闭
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            switch (wifiState) {
                case WifiManager.WIFI_STATE_DISABLED://wifi 关闭
                    Log.d(TAG, "onReceive: wifi 关闭");
                    break;
                case WifiManager.WIFI_STATE_DISABLING:
                    break;
                case WifiManager.WIFI_STATE_ENABLED://wifi 打开
                    break;
                case WifiManager.WIFI_STATE_ENABLING:
                    break;
                case WifiManager.WIFI_STATE_UNKNOWN:
                    break;
            }
        }

        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {//wifi 连接状态
            Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (null != parcelableExtra) {
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                NetworkInfo.State state = networkInfo.getState();
                switch (state) {
                    case CONNECTED://连接
                        break;
                    case UNKNOWN:
                        break;
                    case CONNECTING:
                        break;
                    case DISCONNECTED://断开连接
                        EventBus.getDefault().post(SocketConfig.SOCKET_DIS_CONNECTED);
                        break;
                    case DISCONNECTING:
                        break;
                    case SUSPENDED:
                        break;
                    default:
                        break;
                }
            }
        }
    }

}
